package face.pay.common.enterprise.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.common.enterprise.service.IterminalBaseService;
import face.pay.dao.TerminalInfoDao;
import face.pay.entity.TerminalInfo;
import face.pay.entity.UserInfo;
import face.pay.util.bean.JavaBeanUtils;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import java.util.List;

/**
 * @Auther: zkj
 * @Date: 2019/7/16 17:39
 * @Description:
 */
@Service
@Slf4j
public class TerminalBaseServiceImpl implements IterminalBaseService {

    @Autowired
    TerminalInfoDao terminalInfoDao;

    /**
     * 设备的新增以及修改
     * @param terminalInfo
     * @param loginUser
     * @return
     */
    @Override
    @Transactional
    public ResultMessage edit(TerminalInfo terminalInfo, UserInfo loginUser) {

        Integer id = terminalInfo.getId();
        if(id==null){

            /**
             * 生成deviceId
             */
            String deviceId = null;
            for(Integer i=0;i<=4;i++){

                /**
                 * 获取随机字符串
                 */
                String    nonceStr = JavaBeanUtils.getMixNonceString();

                TerminalInfo ter =  terminalInfoDao.getByDeviceId(nonceStr);

                if(ter==null){
                    deviceId = nonceStr;
                    break;
                }else {
                    continue;
                }
            }

            if(StringUtils.isNotBlank(deviceId)){
                terminalInfo.setDeviceId(deviceId);
                terminalInfo.setOemId(loginUser.getOemId());
                terminalInfo.setAgencyUserId(loginUser.getId());
                terminalInfo.setCreatedBy(loginUser.getId());
                this.terminalInfoDao.insert(terminalInfo);
            }else {
                return ResultMessage.createSuccess("设备编码生成错误，稍后重试");
            }

        }else {
            terminalInfo.setUpdatedBy(loginUser.getId());
            this.terminalInfoDao.update(terminalInfo);
        }

        return ResultMessage.createSuccess();
    }

    @Override
    public PageData queryPage(TerminalInfo terminalInfo, PageParam pageParam, UserInfo loginUser) {
        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());

        terminalInfo.setOemId(loginUser.getOemId());

        List<TerminalInfo> terminalInfos = this.terminalInfoDao.queryDetailList(terminalInfo);
        PageInfo<TerminalInfo> pageInfo = new PageInfo <TerminalInfo>(terminalInfos);

        PageData<TerminalInfo> pageData = new PageData<TerminalInfo>(pageInfo.getTotal(),terminalInfos);

        return pageData;
    }

    @Override
    public ResultMessage getNoBing(Model model, TerminalInfo terminalInfo, UserInfo loginUser) {

        terminalInfo.setOemId(loginUser.getOemId());
        terminalInfo.setUserId(loginUser.getId());
        List<TerminalInfo> terminalInfoList=this.terminalInfoDao.getNoBing(terminalInfo);
        model.addAttribute("terminalList",terminalInfoList);
        return ResultMessage.createSuccess(terminalInfoList);
    }

    @Override
    public ResultMessage storeBing(TerminalInfo terminalInfo, UserInfo loginUser) {
        log.info("设备绑定门店");
        terminalInfo.setUpdatedBy(loginUser.getId());
        this.terminalInfoDao.storeBing(terminalInfo);
        return ResultMessage.createSuccess();
    }

    /**
     * 设备已经安装 登录编码已经绑定 再次登录时候 需要解除绑定
     * @param terminalInfo
     * @param loginUser
     * @return
     */
    @Override
    public ResultMessage unLock(TerminalInfo terminalInfo, UserInfo loginUser) {

        terminalInfo.setUpdatedBy(loginUser.getId());
        this.terminalInfoDao.unBing(terminalInfo);
        return ResultMessage.createSuccess("解除成功");
    }
}
